using System;
using Microsoft.BizTalk.ExplorerOM;

namespace BizUnit.Extensions.BizTalk.Helpers
{
    internal class BizTalkStepHelper
    {
        public const string DEFAULT_CONNECTION_STRING = "Integrated Security=SSPI;database=BizTalkMgmtDb;server=(local)";

        public static BtsCatalogExplorer GetRoot(string connectionString)
        {
            BtsCatalogExplorer root = new BtsCatalogExplorer();

            // Additional check if its an empty connection string. This is useful for consumers
            // as it reduces the code to check for empty connection strings. Of course, it doesnt matter
            // if its called from the overloaded method which supplies the default setting.
            if (String.IsNullOrEmpty(connectionString))
            {
                root.ConnectionString = DEFAULT_CONNECTION_STRING;
            }
            else
            {
                root.ConnectionString = connectionString;
            }
            return root;
        }

        public static BtsCatalogExplorer GetRoot()
        {
            // call the overloaded method
            return GetRoot(DEFAULT_CONNECTION_STRING);
        }

        public static IBizTalkApplication GetApplication(string applicationName, BtsCatalogExplorer root)
        {
            return root.Applications[applicationName];
        }

        public static IBizTalkApplication GetApplication(string applicationName, string connectionString)
        {
            BtsCatalogExplorer root = GetRoot(connectionString);
            return root.Applications[applicationName];
        }

        public static IBizTalkApplication GetApplication(string applicationName)
        {
            return GetApplication(applicationName, DEFAULT_CONNECTION_STRING);

        }

        public static BtsOrchestration GetOrchestration(IBizTalkApplication sourceApp, string orchestrationFullName)
        {
            foreach (BtsOrchestration orch in sourceApp.Orchestrations)
            {
                if (orch.FullName.Equals(orchestrationFullName))
                {
                    return orch;
                }
            }

            return null;
        }

        public static ReceivePort GetReceivePort(IBizTalkApplication sourceApp, string portName)
        {

            foreach (ReceivePort port in sourceApp.ReceivePorts)
            {
                if (port.Name.Equals(portName))
                {
                    return port;
                }
            }

            return null;
        }

        public static ReceiveLocation GetReceiveLocation(ReceivePort port, string locationName)
        {
            foreach (ReceiveLocation location in port.ReceiveLocations)
            {
                if (location.Name.Equals(locationName))
                {
                    return location;
                }
            }

            return null;

        }

        public static SendPort GetSendPort(IBizTalkApplication sourceApp, string portName)
        {

            foreach (SendPort port in sourceApp.SendPorts)
            {
                if (port.Name.Equals(portName))
                {
                    return port;
                }
            }

            return null;
        }

    }
}
